#ifndef STX_ALGORITHM_UNIQUE_HPP_
#define STX_ALGORITHM_UNIQUE_HPP_

#include <stx/algorithm/common.hpp>
#include <stx/algorithm/move.hpp>
#include <stx/algorithm/adjacent_find.hpp>

namespace stx {

template <class ForwardIterator, class BinaryPredicate>
inline ForwardIterator
unique(ForwardIterator first, ForwardIterator last, BinaryPredicate pred)
{
    first = stx::adjacent_find<ForwardIterator, typename add_lvalue_reference<BinaryPredicate>::type>(first, last, pred);
    if (first != last) {
        ForwardIterator i = first;
        for (++i; ++i != last;) {
            if (!pred(*first, *i)) {
                *++first = stx::move(*i);
            }
        }
        ++first;
    }
    return first;
}

template <class ForwardIterator>
inline ForwardIterator
unique(ForwardIterator first, ForwardIterator last)
{
    typedef typename std::iterator_traits<ForwardIterator>::value_type v;
    return stx::unique(first, last, stx::equal_to<v>());
}

} // namespace stx

#endif // STX_ALGORITHM_UNIQUE_HPP_
